#property copyright "Copyright ?2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
int buytisun=200;
int selltisun=200;
int init()
  {
   return(0);
  }
int deinit()
  {
   return(0);
  }
int start()
  {
   double ema15M10=iMA(Symbol(),PERIOD_M15,10,0,MODE_EMA,PRICE_CLOSE,0);
   double ema30M10=iMA(Symbol(),PERIOD_M30,10,0,MODE_EMA,PRICE_CLOSE,0);
   double ema15M20=iMA(Symbol(),PERIOD_M15,20,0,MODE_EMA,PRICE_CLOSE,0);
   double ema30M20=iMA(Symbol(),PERIOD_M30,20,0,MODE_EMA,PRICE_CLOSE,0);
   double k=iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_MAIN,0);
   double d=iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_SIGNAL,0);
   double kp=iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_MAIN,1);
   double dp=iStochastic(Symbol(),PERIOD_M15,8,5,5,MODE_SMA,0,MODE_SIGNAL,1);
   if((ema15M10<ema15M20)&&(ema30M10<ema30M20))//sell
     {
         if((k<d)&&(kp>dp))//
           {
               if(sell(0.1,500,500,Symbol()+"sell",0)>0)
                 {
                   selltisun=200;
                 } 
           }
     }
   if((ema15M10>ema15M20)&&(ema30M10>ema30M20))//buy
     {
         if((k>d)&&(kp<dp))//
           {
               if(buy(0.1,500,500,Symbol()+"buy",0)>0)
                 {
                   buytisun=200;
                 }   
           }
     }
    
    for(int i=0;i<OrdersTotal();i++)
               {
                   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                     {
                       if((OrderComment()==(Symbol()+"buy")))    
                         {
                              if(((Bid-OrderOpenPrice())/Point)>=buytisun)
                                {
                                   double buysl=OrderStopLoss();
                                   if(OrderModify(OrderTicket(),OrderOpenPrice(),buysl+200*Point,OrderTakeProfit(),0)==true)
                                     {
                                        buytisun=buytisun+200;
                                     }
                                }                 
                         } 
                       if((OrderComment()==(Symbol()+"sell")))    
                         {
                              if(((OrderOpenPrice()-Ask)/Point)>=selltisun)
                                {
                                   double sellsl=OrderStopLoss();
                                   if(OrderModify(OrderTicket(),OrderOpenPrice(),sellsl-200*Point,OrderTakeProfit(),0)==true)
                                     {
                                        selltisun=selltisun+200;
                                     }
                                }                 
                         } 
                      }
               }
   return(0);
  }
int buy(double Lots,double sun,double ying,string comment,int magic)
  {
          int kaiguan=0;
            for(int i=0;i<OrdersTotal();i++)
               {
                   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                     {
                       if((OrderComment()==comment)&&(OrderMagicNumber()==magic))    
                         {
                           kaiguan=1;                     
                         } 
                      }
               }
            if(kaiguan==0)
              {
                   int ticket=OrderSend(Symbol( ) ,OP_BUY,Lots,Ask,300,0,0,comment,magic,0,White);
                   if(ticket>0)
                   {
                    if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
                      {
                       if((sun!=0)&&(ying!=0))
                        {
                          OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-sun*MarketInfo(Symbol(),MODE_POINT),OrderOpenPrice()+ying*MarketInfo(Symbol(),MODE_POINT),0,Red); 
                        }
                       if((sun==0)&&(ying!=0))
                        {
                          OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()+ying*MarketInfo(Symbol(),MODE_POINT),0,Red); 
                        }
                       if((sun!=0)&&(ying==0))
                        {
                          OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-sun*MarketInfo(Symbol(),MODE_POINT),0,0,Red); 
                        }
                      }
                   }
              return(ticket);
              }
             else
              {
               return(0);
              }
  }
int sell(double Lots,double sun,double ying,string comment,int magic)
    {
               int kaiguan=0;
                 for(int i=0;i<OrdersTotal();i++)
                    {
                        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                          {
                            if((OrderComment()==comment)&&(OrderMagicNumber()==magic))   
                              {
                                kaiguan=1;                     
                              } 
                           }
                    }
                 if(kaiguan==0)
                   {
                        int ticket=OrderSend(Symbol( ) ,OP_SELL,Lots,Bid,300,0,0,comment,magic,0,Red);
                        if(ticket>0)
                        {
                          if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
                           {
                             if((sun!=0)&&(ying!=0))
                              {
                               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sun*MarketInfo(Symbol(),MODE_POINT),OrderOpenPrice()-ying*MarketInfo(Symbol(),MODE_POINT),0,Red);
                              } 
                             if((sun==0)&&(ying!=0))
                              {
                               OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()-ying*MarketInfo(Symbol(),MODE_POINT),0,Red);
                              } 
                             if((sun!=0)&&(ying==0))
                              {
                               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sun*MarketInfo(Symbol(),MODE_POINT),0,0,Red);
                              } 
                           }
                        }
                        return(ticket);
                   }
                  else
                   {
                    return(0);
                   } 
  }